package com.smiier.skin;

import java.util.Calendar;
import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.ui.ODatePicker;
import cn.o.app.ui.ODatePicker.OnPickDateListener;
import cn.o.app.util.ODateFormat;
import cn.skinapp.R;

import com.evan.common.constant.Constant;
import com.evan.common.widget.ItemView;
import com.smiier.skin.extra.TreatmentProjectItemExtra;
import com.smiier.skin.net.ConfirmCodeGetTask;
import com.smiier.skin.net.ConfirmCodeGetTask.ConfirmCodeGetRequest;
import com.smiier.skin.net.ConfirmCodeGetTask.ConfirmCodeGetResponse;
import com.smiier.skin.net.DoctorReserveCreateTask;
import com.smiier.skin.net.DoctorReserveCreateTask.DoctorReserveCreateRequest;
import com.smiier.skin.net.DoctorReserveCreateTask.DoctorReserveCreateResponse;
import com.smiier.skin.net.DoctorTreatmentProjectListTask.DoctorTreatmentProjectItem;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class TreatmentReserveActivity extends BasicActivity implements
		OnClickListener {
	EditText mReserveName;
	String mSex = "男";
	EditText mPhone;
	EditText mPhoneCheck;
	RadioButton mFmale;
	RadioButton mMale;
	Button mAge;
	Button mGetCheck;
	ItemView mReserveTime;
	TextView mCreateReserve;
	static int ConfirmCodeType = 8;
	String mTel;
	String mAges;
	Date mDate;
	DoctorTreatmentProjectItem item;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_treatment_reserve);
		init();
		setNavTitle("预约");
		TreatmentProjectItemExtra extra = new TreatmentProjectItemExtra();
		Intent i = getIntent();
		extra.getFrom(i);
		item = extra.getUser();
		mReserveName = findViewById(R.id.reserve_name, EditText.class);
		mPhone = findViewById(R.id.phone, EditText.class);
		mPhoneCheck = findViewById(R.id.phone_check, EditText.class);
		mMale = findViewById(R.id.radio_male, RadioButton.class);
		mFmale = findViewById(R.id.radio_fmale, RadioButton.class);
		mReserveTime = findViewById(R.id.treatment_reserve_time, ItemView.class);
		mCreateReserve = findViewById(R.id.create_reserve, TextView.class);
		mAge = findViewById(R.id.btn_age, Button.class);
		mGetCheck = findViewById(R.id.btn_get_check, Button.class);
		mCreateReserve.setOnClickListener(this);
		mAge.setOnClickListener(this);
		mGetCheck.setOnClickListener(this);
		mAges = CommonUtility.getAgeByBirthday(GlobalSettings.sUser.Birth
				.toString());
		mAge.setText("年龄：" + mAges + "岁");
	}

	Handler h = new Handler() {
		public void handleMessage(android.os.Message msg) {
			mGetCheck.setEnabled(true);
			mGetCheck.setText("获取验证码");
		};
	};

	@Override
	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		if (id == R.id.create_reserve) {
			String name = mReserveName.getText().toString();
			mTel = mPhone.getText().toString();
			String mCheck = mPhoneCheck.getText().toString();
			if (mFmale.isChecked()) {
				mSex = "男";
			}
			if (mMale.isChecked()) {
				mSex = "女";
			}
			if (name == null || name.trim().equals("")) {
				toast("请输入您的名字");
				return;
			}
			if (mTel == null || mTel.trim().equals("")) {
				toast("请输入您的电话号码");
				return;
			}
			if (mCheck == null || mCheck.trim().equals("")) {
				toast("请输入您的验证码");
				return;
			}
			if (mDate == null) {
				toast("请选择您的预约时间");
				return;
			}
			if (mDate.getTime() < System.currentTimeMillis()) {
				toast("请选择有效的预约时间");
				return;
			}
			createReserve(name, mCheck, mTel, mTime, mSex);
		} else if (id == R.id.btn_age) {
			ODatePicker datePicker = new ODatePicker(getContext());
			datePicker.show();
			datePicker.setListener(new OnPickDateListener() {

				@Override
				public void onPicked(ODatePicker picker, Date date, String time) {
					String age = CommonUtility.getAgeByBirthday(ODateFormat
							.format(date, "yyyy"));
					mAges = age;
					mAge.setText("年龄：" + age + "岁");
				}
			});
		} else if (id == R.id.btn_get_check) {
			mTel = mPhone.getText().toString();
			if (!CommonUtility.isNull(mTel)) {
				time = 120;
				getSmsCode();
				mGetCheck.setEnabled(false);
				mGetCheck.setTextColor(getResources().getColor(
						R.color.text_hint));
				mGetCheck.setText("120秒后重发");
				h.sendEmptyMessageAtTime(0, 120000);
				new Thread(runTimeThread).start();
			} else {
				toast("号码不能为空");
			}

		} else if (id == R.id.treatment_reserve_time) {
			ODatePicker datePicker = new ODatePicker(getContext());
			Calendar cal = Calendar.getInstance();
			datePicker.setStartYear(cal.get(Calendar.YEAR));
			datePicker.setEndYear(cal.get(Calendar.YEAR) + 1);
			datePicker.setCircleYear(false);
			datePicker.setmPickAmPm(true);
			datePicker.show();
			datePicker.setListener(new OnPickDateListener() {
				@Override
				public void onPicked(ODatePicker picker, Date date, String time) {
					mTime = ODateFormat.format(date, "yyyy-MM-dd");
					mReserveTime.setTextTipValue(mTime + time);
					mAfternoon = time;
					mDate = date;

				}
			});
		}
	}

	String mTime;
	String mAfternoon;
	Handler timerHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			mGetCheck.setEnabled(false);
			mGetCheck.setTextColor(getResources().getColor(R.color.text_hint));
			mGetCheck.setText(msg.what + "后重新发送");

		}
	};
	int time = 120;
	Runnable runTimeThread = new Runnable() {

		@Override
		public void run() {
			while (time > 0) {
				time--;

				try {
					Thread.sleep(1000);
					timerHandler.sendEmptyMessage(time);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	};

	void createReserve(String name, String mCheck, String mTel2, String mDate2,
			String mSex2) {
		DoctorReserveCreateTask task = new DoctorReserveCreateTask();
		DoctorReserveCreateRequest req = new DoctorReserveCreateRequest();
		req.username = name;
		req.mobile = mTel2;
		req.time = mDate2 + " " + (mAfternoon.equals("上午") ? "不是" : "是");
		req.sex = mSex2;
		req.age = Integer.parseInt(mAges);
		req.treatmentprojectid = item.id + "";
		req.validatecode = mCheck;
		req.userid = GlobalSettings.sUid + "";
		req.client_id = GlobalSettings.sUser.GT_ClientID;
		task.setRequest(req);
		task.addListener(new NetTaskListener<DoctorReserveCreateTask.DoctorReserveCreateRequest, DoctorReserveCreateTask.DoctorReserveCreateResponse>() {
			@Override
			public void onComplete(
					INetTask<DoctorReserveCreateRequest, DoctorReserveCreateResponse> task,
					DoctorReserveCreateResponse response) {
				if (response != null && response.ResultCode == 200) {
					// toast("预约成功");
					Intent i = new Intent(TreatmentReserveActivity.this,
							CreateQuestionSuccessfullyActivity.class);
					i.putExtra(Constant.IDENTITY_KEY, true);
					TreatmentProjectItemExtra extra = new TreatmentProjectItemExtra();
					extra.setUser(item);
					if (extra.putTo(i)) {
						startActivity(i);
						finish();
					}
				} else {
					toast(response.ResultMessage);
				}
			}

			@Override
			public void onException(
					INetTask<DoctorReserveCreateRequest, DoctorReserveCreateResponse> task,
					Exception e) {
			}
		});
		add(task);
	}

	void getSmsCode() {
		if (!CommonUtility.isNull(mTel)) {
			ConfirmCodeGetRequest req = new ConfirmCodeGetRequest();
			req.mobile = mTel;
			req.ConfirmCodeType = ConfirmCodeType;
			ConfirmCodeGetTask task = new ConfirmCodeGetTask();
			task.setRequest(req);
			task.addListener(new NetTaskListener<ConfirmCodeGetTask.ConfirmCodeGetRequest, ConfirmCodeGetTask.ConfirmCodeGetResponse>() {

				@Override
				public void onComplete(
						INetTask<ConfirmCodeGetRequest, ConfirmCodeGetResponse> task,
						ConfirmCodeGetResponse response) {

					if (response.ResultCode == 200
							&& response.Res == GlobalSettings.IS_TRUE) {
						toast("验证码发送成功");
					}
				}

				@Override
				public void onException(
						INetTask<ConfirmCodeGetRequest, ConfirmCodeGetResponse> task,
						Exception e) {

				}

			});
			add(task);
		} else {
			mGetCheck.setEnabled(true);
			mGetCheck
					.setTextColor(getResources().getColor(R.color.border_blue));
			toast("请输入手机号");
		}
	}
	
/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
}
